package com.maduixa.guitar.tabsearch.spring.business.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.maduixa.guitar.tabsearch.utils.EncryptionUtils;


@Entity
@Table(name="GUITAR_USER")
public class GuitarUser {
	
	@Id
	@Column(name="USERNAME")
	private String username;
	
	@Column(name="PASSWORD")
	private byte[] password;
	
	@Column(name="MAIL")
	private String mail;
	
	@Column(name="DATA_ALTA")
	private Date dataAlta;
	
	@Column(name="ADMIN")
	private boolean admin;
	
	
	/*
	 * Constructors
	 */
	
	public GuitarUser() {
		super();
	}
	
	public GuitarUser(String username, String password, String mail, boolean admin) {
		super();
		this.username = username;
		try {
			this.password = EncryptionUtils.encrypt(password);
		} catch (Exception e) {
			this.password = null;
		}
		this.mail = mail;
		this.dataAlta = new Date();
		this.admin = admin;
	}
	
	/*
	 * Getters & Setters
	 */

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public byte[] getPassword() {
		return password;
	}

	public void setPassword(byte[] password) {
		this.password = password;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public Date getDataAlta() {
		return dataAlta;
	}

	public void setDataAlta(Date dataAlta) {
		this.dataAlta = dataAlta;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	
	/*
	 * Functions
	 */
	
	public String getPasswordStr() {	
		try {
			return EncryptionUtils.decrypt(this.password);
		} catch (Exception e) {
			return null;
		}
	}

	public void setPasswordStr(String password) {
		try {
			this.password = EncryptionUtils.encrypt(password);
		} catch (Exception e) {
			this.password = null;
		}
	}
	
	public boolean login(String password) {
		
		String decr;
		
		try {
			decr = EncryptionUtils.decrypt(this.password);
		} catch (Exception e) {
			return false;
		}
		
		if (decr.equals(password)) return true;
		else return false;
	}
	
}